package com.hwn.datax.controller;

import cn.hutool.core.util.StrUtil;
import com.hwn.datax.domain.datax.RangeOption;
import com.hwn.datax.utils.AlertUtil;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wnhuang
 * @date 2024/6/8 3:20
 */
@Controller
public class BatchImportController {
    @FXML
    public TextArea valueTextArea;
    @FXML
    public Button parseBtn;
    @FXML
    public Button importBtn;
    @FXML
    public Button cancelBtn;

    private ObservableList<String> leftItems;

    private ObservableList<String> rightItems;

    private ObservableList<String> targetItems;


    public void init(ObservableList<String> leftItems, ObservableList<String> rightItems, ObservableList<String> targetItems) {
        this.targetItems = targetItems;
        this.leftItems = leftItems;
        this.rightItems = rightItems;
    }


    public void initialize() {
        parseBtn.setOnAction(event -> {
            parseImportStr();
        });
        importBtn.setOnAction(event -> {
            batchImportByStr(valueTextArea.getText());
        });
        cancelBtn.setOnAction(event -> {
            handleCancel();
        });
    }

    private void handleCancel() {
        Stage stage = (Stage) cancelBtn.getScene().getWindow();
        stage.close();
    }

    private String matchOggStr(String lineStr) {
        String table = lineStr.trim();
        // 使用正则表达式匹配表名
        Pattern regex = Pattern.compile("^TABLE\\s+\\w+\\.(\\w+)\\s*;", Pattern.CASE_INSENSITIVE);
        Matcher matcher = regex.matcher(table);
        if (matcher.find()) {
            return matcher.group(1);
        }
        Pattern regex1 = Pattern.compile("^MAP\\s+\\w+\\.(\\w+)\\s*,", Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = regex1.matcher(table);
        if (matcher1.find()) {
            return matcher1.group(1);
        }
        return null;
    }

    public void parseImportStr() {
        String importStr = valueTextArea.getText();
        // 将字符串分割成行
        String[] lines = importStr.split("\n");

        // 遍历每一行
        StringBuilder newLines = new StringBuilder();
        for (String line : lines) {
            String newStr = matchOggStr(line);
            // 判断是否符合条件
            if (newStr != null) {
                // 替换当前行
                newLines.append(newStr).append("\n");
            } else {
                // 返回原始行
                newLines.append(line).append("\n");
            }
        }

        // 将新行连接成字符串
        importStr = newLines.toString();

        valueTextArea.setText(importStr);
    }

    public void batchImportByStr(String importStr) {
        int index = 0;
        String[] lines = importStr.split("\n");
        for (String item : lines) {
            String table = item.trim();
            if (StrUtil.isBlank(table)) {
                continue;
            }


            List<String> leftList = leftItems.stream().filter(t -> t.equalsIgnoreCase(table)).toList();
            if (leftList.isEmpty()) {
                continue;
            }
            List<String> rightList = rightItems.stream().filter(t -> t.equalsIgnoreCase(table)).toList();
            if (!rightList.isEmpty()) {
                continue;
            }

            List<String> targetList = targetItems.stream().filter(t -> t.equalsIgnoreCase(table)).toList();
            if (targetList.isEmpty()) {
                continue;
            }
            index++;
            rightItems.add(leftList.get(0));
            leftItems.remove(leftList.get(0));

        }
        AlertUtil.showSuccessAlert("导入成功" + index + "张表");
        handleCancel();
    }

}
